﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// Authors Patrick Boos, Michael Gfeller, Silvan Gehrig
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.Surface.Presentation.Controls;
using ScrumTable.Common.Collections;
using ScrumTable.Common.Unity;
using ScrumTable.Config.ConfigManagement;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.UI.View.Util;
using ScrumTable.UI.View.WinTouch.UserControls.View;
using ScrumTable.UI.View.WinTouch.Util;
using ScrumTable.UI.ViewModel;
using ScrumTable.UI.ViewModel.Controller;
using ScrumTable.UI.ViewModel.Data;
using Size = System.Windows.Size;

#endregion

namespace ScrumTable.UI.View.WinTouch.UserControls
{
    /// <summary>
    /// Shows a User Story as a Stack: Shows the User-Story and all childs (tasks)
    /// 
    /// </summary>
    public partial class BoardBugControl : ScrumUserControl
    {


        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private ScrumViewModelData _scrumData;
        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------


        /// <summary>
        /// DependencyProperty for UserStorie.
        /// </summary>
        public static readonly DependencyProperty IsExpanedProperty =
            DependencyProperty.Register("IsExpaned",
                                        typeof(bool),
                                        typeof(BoardBugControl),
                                        new UIPropertyMetadata(false));

        /// <summary>
        /// Shows the control in a expaned mode, or not...
        /// </summary>
        /// <value></value>
        public bool IsExpaned
        {
            get { return (bool)GetValue(IsExpanedProperty); }
            set { SetValue(IsExpanedProperty, value); }
        }

        public IEnumerable<ScrumState> States
        {
            get
            {
                return ElementManager.GetStates(ElementManager.GetTargetConfig(NavigationTarget.DailyScrumBoard.ToString()));
            }
        }

        #endregion


        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------


        /// <summary>
        /// Initializes a new instance of the <see cref="BoardBugControl"/> class.
        /// </summary>
        public BoardBugControl()
        {
            _scrumData = ((IAppScreen)Application.Current.MainWindow).Container.Resolve<ScrumViewModelData>();
            DataContext = this;
            InitializeComponent();
            FirstColumn.Width = new GridLength(StackedDailyScrumStoryBoard.LengthFirstColumn);
            CreateDynamicPart();
        }

        
      
        #endregion
        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Releases managed (c# or .net) resources.
        /// </summary>
        protected override void ReleaseManagedResources()
        {
           base.ReleaseManagedResources();
        }

        private void CreateDynamicPart()
        {
            int i = 1;
            foreach (var state in States)
            {
                Grid.ColumnDefinitions.Add(new ColumnDefinition());
                StackedBugControl control = new StackedBugControl() {NumbersOfOtherControls = States.Count(), Margin = new Thickness(5, 0, 5, 0), HorizontalAlignment = HorizontalAlignment.Stretch, HorizontalContentAlignment = HorizontalAlignment.Stretch, Background = Resources["ItemBrush"] as Brush };
                Grid.SetColumn(control, i);

                BindingUtil.BindSourcePath(control, StackedBugControl.IsExpanedProperty, "IsExpaned", DataContext);

                control.DataContext = DataContext;
                control.BugState = state;
                //hide non bug states
                if (!_scrumData.CurrentProject.Types.BugType.StateMappings.Contains(state))
                {
                    control.Visibility = Visibility.Collapsed;
                }
                Grid.Children.Add(control);

                i++;
            }
        }
        #endregion

   
     
       
        #region events
       
        
        private void OnAddBugClick(object sender, RoutedEventArgs e)
        {
            
            if (_scrumData.CurrentIteration != null)
            {
                var bug = _scrumData.CurrentIteration.Bugs.AddNew();
                var bugBack = new BugControlBack() { DataContext = new BugViewModel(bug) };
                ((AppScreen)Application.Current.MainWindow).AddScatteredItem(bugBack, ((SurfaceButton)sender).PointToScreen(new Point(0, 0)), true);
            }
        }


       
        #endregion

    }
}
